package cn.clq.ssmshiro.util;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

@Component
public class JedisUtil {
    @Resource
    private JedisPool jedisPool;

    public Jedis getResource(){
        return jedisPool.getResource();
    }

    public byte[] set(byte[] key,byte[] value){
        System.out.println("设置Redis数据");
        Jedis jedis = getResource();
        try {
            jedis.set(key,value);
            return value;
        }finally {
            jedis.close();
        }
    }

    public byte[] get(byte[] key){
        System.out.println("获取Redis数据");
        Jedis jedis = getResource();
        try {
            return jedis.get(key);
        }finally {
            jedis.close();
        }
    }

    public void expire(byte[] key,int seconds){
       Jedis jedis= jedisPool.getResource();
       try {
           jedis.expire(key,seconds);
       }finally {
           jedis.close();
       }
    }

    public byte[] del(byte[] key){
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
            return get(key);
        }finally {
            jedis.close();
        }
    }

    public void hset(byte[] hkey,byte[] key,byte[] value){
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.hset(hkey,key,value);
        }finally {
            jedis.close();
        }
    }
    public byte[] hget(byte[] hkey,byte[] key){
        Jedis jedis = jedisPool.getResource();
         try {
             return jedis.hget(hkey,key);
         }finally {
             jedis.close();
         }
    }
}
